package com.dnjn.happiness.emo.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.core.constant.Constants;
import com.dnjn.happiness.common.core.constant.HappyConstants;
import com.dnjn.happiness.common.core.constant.SecurityConstants;
import com.dnjn.happiness.common.core.domain.R;
import com.dnjn.happiness.common.core.utils.bean.BeanUtils;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.log.annotation.Log;
import com.dnjn.happiness.common.log.enums.BusinessType;
import com.dnjn.happiness.common.security.annotation.InnerAuth;
import com.dnjn.happiness.emo.api.domain.UserModelScore;
import com.dnjn.happiness.emo.domain.vo.UserModelScoreVO;
import com.dnjn.happiness.emo.service.IUserModelScoreService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.validation.Schema;
import java.util.Arrays;
import java.util.List;

/**
 * userModelScoreController
 *
 * @author amber
 * @date 2024-03-02
 */
@Tag(name = "emo userModelScore userModelScore", description = "userModelScore")
@RestController
@RequestMapping("/userModelScore")
public class UserModelScoreController extends BaseController {
    @Autowired
    private IUserModelScoreService userModelScoreService;

    @InnerAuth
    @GetMapping("/getUserModelScoreByUserIdAndType/{userId}/{rtype}")
    public R<UserModelScore> getUserModelScoreByUserIdAndType(@PathVariable Long userId, @PathVariable String rtype) {
        return R.ok(userModelScoreService.lambdaQuery()
                .eq(UserModelScore::getUserId, userId)
                .eq(UserModelScore::getRtype, rtype)
                .eq(UserModelScore::getDeleted, "0")
                .one());
    }

    @InnerAuth
    @GetMapping("/getuserModelScoreList/{userId}")
    public R<List<UserModelScore>> getuserModelScoreList(@PathVariable Long userId) {
        return R.ok(userModelScoreService.lambdaQuery()
                .eq(UserModelScore::getDeleted, Constants.UNDELETED)
                .eq(UserModelScore::getUserId, userId).list());
    }


    @InnerAuth
    @GetMapping("/updateUserModel/{userId}")
    void updateUserModel(@PathVariable("userId") Long userId){

        userModelScoreService.updateUserModelScoreByUserId(userId);
    }

    @InnerAuth
    @PutMapping("/updateUserModelScoreList")
    public void updateUserModelScoreList(@RequestBody UserModelScore item) {
        userModelScoreService.updateUserModelScoreList(item);
    }

    @InnerAuth
    @PutMapping("/updateUserModelScore")
    public void updateUserModelScore(@RequestBody UserModelScoreVO vo) {
        userModelScoreService.updateUserModelScore(vo);
    }

    @InnerAuth
    @GetMapping("/getListByUserId/{userId}")
    public R<List<UserModelScore>> getListByUserId(@PathVariable Long userId) {
        return R.ok(userModelScoreService.lambdaQuery()
                .eq(UserModelScore::getDeleted, Constants.UNDELETED)
                .eq(UserModelScore::getUserId, userId).ne(UserModelScore::getRtype, HappyConstants.EMO_USER_RTYPE_ATTENDANCE)
                .ne(UserModelScore::getRtype, HappyConstants.EMO_USER_RTYPE_COMM).list());
    }

    @GetMapping("/getByUserIdAndRtype")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    public AjaxResult getByUserIdAndRtype(@RequestParam Long userId, @RequestParam String rtype) {
        UserModelScore po = userModelScoreService.lambdaQuery()
                .eq(UserModelScore::getUserId, userId)
                .eq(UserModelScore::getRtype, rtype)
                .eq(UserModelScore::getDeleted, "0")
                .one();

        return AjaxResult.success(po);
    }


    /**
     * 查询userModelScore列表
     */
    //@RequiresPermissions("@ss.hasPermi('emo:userModelScore:list')")
    @PostMapping("/list")
    @Log(title = "emo", businessType = BusinessType.LIST)
    public AjaxResult list(@RequestBody UserModelScoreVO vo, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        UserModelScore userModelScore = new UserModelScore();
        BeanUtils.copyPropsIgnoreNull(vo, userModelScore);
        Page page = Page.of(current, size);
        Page<UserModelScore> list = userModelScoreService.page(page, Wrappers.query(userModelScore));
        return success(list);
    }

    /**
     * 获取userModelScore详细信息
     */
    //@RequiresPermissions("@ss.hasPermi('emo:userModelScore:query')")
    @GetMapping(value = "/{id}")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    public AjaxResult get(@PathVariable("id") Long id) {
        return success(userModelScoreService.getById(id));
    }

    /**
     * 新增userModelScore
     */
    @Log(title = "userModelScore", businessType = BusinessType.INSERT)
    //@RequiresPermissions("@ss.hasPermi('emo:userModelScore:add')")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody UserModelScoreVO vo) {
        UserModelScore userModelScore = new UserModelScore();
        BeanUtils.copyPropsIgnoreNull(vo, userModelScore);

        return toAjax(userModelScoreService.save(userModelScore));
    }

    /**
     * 修改userModelScore
     */
    @Log(title = "userModelScore", businessType = BusinessType.UPDATE)
    //@RequiresPermissions("@ss.hasPermi('emo:userModelScore:edit')")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody UserModelScoreVO vo) {
        UserModelScore userModelScore = new UserModelScore();
        BeanUtils.copyPropsIgnoreNull(vo, userModelScore);
        return toAjax(userModelScoreService.updateById(userModelScore));
    }

    /**
     * 删除userModelScore
     */
    @Log(title = "userModelScore", businessType = BusinessType.DELETE)
    //@RequiresPermissions("@ss.hasPermi('emo:userModelScore:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userModelScoreService.removeBatchByIds(Arrays.asList(ids)));
    }
}
